package servlet;

public class LoginServlet {
}
        import cn.itcast.chapter08.entity.User;

        import javax.servlet.*;
        import javax.servlet.http.*;
        import javax.servlet.annotation.*;
        import java.io.IOException;

@WebServlet(name = "servlet.LoginServlet", value = "/servlet.LoginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username =request.getParameter("username");
        String password =request.getParameter("password");
        if("itcast".equals(username)&&"123456".equals(password)){
            User user =new User();
            user.setUsername(username);
            user.setPassword(password);
            request.getSession().setAttribute("user",user);
            String autologin = request.getParameter("autologin");
            if(autologin !=null){
                Cookie cookie=new Cookie("autologin",username+"-"+password);
                cookie.setMaxAge(Integer.parseInt(autologin));
                cookie.setPath(request.getContextPath());
                response.addCookie(cookie);
            }
            response.sendRedirect(request.getContextPath()+"/index0.jsp");
        }else{
            request.setAttribute("errerMsg","用户名或密码错误");
            request.getRequestDispatcher("/login.jsp").forward(request,response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}